/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.client.container.gui;

import java.util.Collection;
import java.util.Vector;
import org.toools.gwt.widget.client.bc.BusinessObjectPanel;
import org.toools.gwt.widget.client.bc.StringAttribute;
import org.toools.gwt.wmc.jpa.CompanyPart;
import org.toools.gwt.wmc.jpa.Container;

/**
 *
 * @author Manuel
 */
public class ContainerPanel extends BusinessObjectPanel {

    StringAttribute name = null;

    Collection<CompanyPart> parents = new Vector<CompanyPart>() {};

    public ContainerPanel(Object bp) {
        super(bp, 2);

        name = new StringAttribute("name", false);
        name.setMaxLength(50);

        this.add( name );

        recreateContent();
        preselect();
        validate();

        setStatus("* missing or invalid input");
    }

    public void addParent(CompanyPart cp) {
        this.parents.add(cp);
    }

    public void preselect() {
        if (getBusinessObject() != null) {
            Container a = (Container) getBusinessObject();
            name.setAttributeValue(a.getName());
        } else {
            name.setAttributeValue("");
        }
    }

    public void map() {
        if (getBusinessObject() == null) {
            setBusinessObject(new Container());
        }
        Container cont = (Container) getBusinessObject();
        cont.setName("" + name.getAttributeValue());
        if(parents.size() > 0) {
            cont.setCompanyParts(parents);
        }
    }

}
